<?php

use Welcomescreen\Agavi\View;

class Backend_Visits_ListSuccessView extends View\BackendBaseView {
	
	/**
	 * @parameter  AgaviRequestDataHolder $rd
	 * @return     mixed
	 */
	public function executeHtml(AgaviRequestDataHolder $rd) {
		$this->setupHtml($rd);
		
		$templateObjects = $this->getContext()->getModel('TemplateManager')->getTemplates();
		$templateArray = array();
		foreach($templateObjects as $template) {
			$variables = array();
			foreach($template->getVariables() as $variable) {
				$variables[] = $variable->toArray();
			}
			$templateArray[$template->getName()] = array(
				'name' => $template->getName(),
				'variables' => $variables,
			);
		}
		$this->setAttribute('templateNames', array_keys($templateObjects));
		$this->setAttribute('templatesJson', json_encode($templateArray));
		$this->setAttribute('visitsJson', $this->getVisitsJson($rd));
	}
	
	/**
	 * @parameter  AgaviRequestDataHolder $rd
	 * @return     mixed
	 */
	public function executeJson(AgaviRequestDataHolder $rd) {
		return $this->getVisitsJson($rd);
	}
	
	
	/**
	 * Retrieves the visits as json string using "items" as parent key.
	 * 
	 * @param AgaviRequestDataHolder $rd
	 * @return string
	 */
	protected function getVisitsJson(AgaviRequestDataHolder $rd) {
		$visits = array();
		foreach($this->getAttribute('visits') as $visit) {
			$visits[] = $visit->toArray();
		}
		return json_encode(array('items' => $visits));
	}
}

?>